/**
 * 
 */
package com.imonsoft.common.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.CollectionFactory;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

/**
 * 
 * @author LiQianHui
 * @date 2013-04-17
 * @version 2013
 */
public abstract class MapUtil {
	protected final static Log logger = LogFactory.getLog(MapUtil.class);

	public static int getInt(Map<?, ?> map, String p) {
		Object o = map.get(p);
		if (o != null) {
			return Integer.parseInt(o.toString());
		}
		throw new RuntimeException("参数转换为int异常！");
	}

	/**
	 * 
	 * @param map
	 * @param key
	 * @return
	 */
	public static String getString(Map<?, ?> map, String key) {
		if (CollectionUtils.isEmpty(map)) {
			return "";
		}
		Object o = map.get(key);
		if (o == null) {
			return "";
		} else {
			return o.toString();
		}
	}

	public static List<?> getList(Map<?, ?> map, String key) {
		Object o = map.get(key);
		if (o instanceof List) {
			return (List<?>) o;
		}
		return null;
	}

	public static Map<?, ?> getMap(Map<?, ?> map, String key) {
		Object o = map.get(key);
		if (o instanceof Map) {
			return (Map<?, ?>) o;
		}
		return null;
	}

	/**
	 * 
	 * @param map
	 *            {key:value,key2:value,...}
	 * @return "key,key2,..."
	 */
	public static String getKeys(Map<String, Object> map) {
		Assert.notNull(map, "map must not be null");
		Iterator<String> keys = map.keySet().iterator();
		StringBuilder sb = new StringBuilder();
		while (keys.hasNext()) {
			String key = (String) keys.next();
			sb.append(key).append(",");
		}
		if (sb.length() > 0) {
			return sb.substring(0, sb.length() - 1);
		}
		return null;
	}

	/**
	 * 
	 * @param map
	 *            {key:value,key2:value2,...}
	 * @param keys
	 *            "key,key2,..."
	 * @return [value,value2,...]
	 */
	public static Object[] getValues(Map<String, Object> map, Object keys) {
		Assert.notNull(map, "map must not be null");
		Assert.notNull(keys, "keys must not be null");
		Object[] values = null;
		if (keys instanceof String) {
			String strKeys = (String) keys;
			strKeys = strKeys.replaceAll(":date", "").toLowerCase();
			String[] ks = strKeys.split(",");
			values = new Object[ks.length];
			for (int i = 0; i < ks.length; i++) {
				// 编码转换
				values[i] = StringUtil.fixup(map.get(ks[i].trim()));
			}
		} else if (keys instanceof String[]) {
			String[] arrKeys = (String[]) keys;
			values = new Object[arrKeys.length];
			for (int i = 0; i < arrKeys.length; i++) {
				// 编码转换
				values[i] = StringUtil.fixup(map.get(arrKeys[i]
						.replaceAll(":date", "").toLowerCase().trim()));
			}
		}
		return values;
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	public static void main(String[] args) {
		Map<String, Object> map = CollectionFactory
				.createLinkedCaseInsensitiveMapIfPossible(3);
		map.put("DATES", "2007-01-01");
		map.put("STATUS", "0");
		System.out.println(getValues(map,
				new String[] { "DATES:date", "STATUS" })[1].toString());
	}
}
